<template>
<div class="body">
    <div class="pwd">
    <el-form class="user-account-key" ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item style="font-weight: 700;" label="原密码:" prop="password">
            <el-input type="password" placeholder="请输入原密码" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item style="font-weight: 700;" label="新密码:" prop="newPassword">
            <el-input type="password" placeholder="请设置新密码" v-model="form.newPassword"></el-input>
        </el-form-item>
        <el-form-item style="font-weight: 700;" label="确认密码:" prop="newPassword2">
            <el-input type="password" placeholder="请确认新密码" v-model="form.newPassword2"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit" class="button">保存</el-button>
            <el-button @click="$refs['form'].resetFields()" class="button">重置</el-button>
        </el-form-item>
    </el-form>
    </div>
</div>
</template>

<script scoped>
export default {
    data () {
        let validateNewPassword = (rule, value, callback) => {
            if (value === this.form.password) {
                callback(new Error('新密码不能与原密码相同!'))
            } else {
                callback()
            }
        }
        let validateNewPassword2 = (rule, value, callback) => {
            if (value !== this.form.newPassword) {
                callback(new Error('与新密码不一致!'))
            } else {
                callback()
            }
        }
        return {
            form: {},
            rules: {
                password: [
                    { required: true, message: '请输入原密码', trigger: 'blur' }
                ],
                newPassword: [
                    { required: true, message: '请设置新密码', trigger: 'blur' },
                    { validator: validateNewPassword, trigger: 'blur' }
                ],
                newPassword2: [
                    { required: true, message: '请确认新密码', trigger: 'blur' },
                    { validator: validateNewPassword2, trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
      onSubmit(){
        let id = this.shuju._id
        this.axios.post('/api/changePwd', { userNewPwd: this.form.newPassword, userOldPwd: this.form.password })
        .then(res => {
            this.$store.state.user.password= this.form.newPassword
            alert("修改成功")
            this.$router.push('/login')
        })
      }
    },
    computed:{
    shuju(){
      console.log('111',this.$store.state.user.password);
      console.log('222',this.form.newPassword);
      return this.$store.state.user.password
    }
  }
}
</script>
<style lang="scss" scoped>
.pwd{
    width: 700px;
    background-color: rgba(255, 255, 255, 0.6);
    margin: 200px auto;
    padding: 30px 10px;
}
.body {
  position: absolute;
  width: 100%;
  height: 100%;
  background: url("https://ts1.cn.mm.bing.net/th/id/R-C.99cf4b0c5da6eb968bbd521dfccae642?rik=qnc2dX43vr0jqw&riu=http%3a%2f%2fwww.csgo.com.cn%2fjpg%2f170616%2f10691497595300638.jpg&ehk=NFtYVbrSfYrpWMv9a0Z6EsP8oCXbLYR%2fVcNrR5nBpAs%3d&risl=&pid=ImgRaw&r=0") no-repeat center top;
  background-size:cover;
  background-attachment:fixed;
  background-color:#CCCCCC;
}
.user-account-key{
    margin: 20px;
}
.button{
    margin-left: -120px;
}
</style>